package com.changgou.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/*****
 * @Author: 黑马训练营
 * 实现文件上传、文件下载、文件修改等功能
 ****/
public class FastDFSClient {


    /****
     * 初始化Tracker的链接信息
     * 实现文件上传
     * 必须知道Tracker的链接IP和端口
     */
    static {
        try {
            //1.读取文件
            String filePath = new ClassPathResource("fdfs_client.conf").getPath();//类路径下的文件名字
            //2.使用FastDFS客户端初始化加载数据
            ClientGlobal.init(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /****
     * 文件上传
     */
    public static String[] upload(FastDFSFile file) throws Exception{
        //自定义扩展参数
        NameValuePair[] meta_list = new NameValuePair[2];
        meta_list[0]=new NameValuePair("设备","HUAWEI Mate 30 Pro 5G");
        meta_list[1]=new NameValuePair("地点","中南海");

        //1、请求TrackerServer
        StorageClient storageClient = getStorageClient();

        /***
         * 4、根据存储的Storage信息实现文件上传
         * 1:文件的字节数组
         * 2:文件的扩展名(后缀,jpg,mp4)
         * 3:自定义扩展参数
         */
        String[] backlist = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);

        return backlist;
    }


    /***
     * 获取文件信息
     */
    public static FileInfo getFile(String groupName, String remoteFileName) throws Exception{
        //1、请求TrackerServer
        StorageClient storageClient = getStorageClient();

        /***
         * 4、获取文件信息
         * 1:文件所属的组名
         * 2:文件的存储路径信息
         */
        return storageClient.get_file_info(groupName,remoteFileName);
    }


    /***
     * 文件下载-Storage
     * @throws Exception
     */
    public static InputStream downFile(String groupName, String remoteFileName)throws Exception{
        StorageClient storageClient = getStorageClient();

        /***
         * 4、文件下载
         * 1:文件所属的组名
         * 2:文件的存储路径信息
         **/
        byte[] bytes = storageClient.download_file(groupName, remoteFileName);

        //将字节数组转换成InputStream
        ByteArrayInputStream is = new ByteArrayInputStream(bytes);
        return is;
    }

    /***
     * 抽取获取StorageClient
     * @return
     * @throws IOException
     */
    public static StorageClient getStorageClient() throws IOException {
        //1、请求TrackerServer
        TrackerClient client = new TrackerClient();

        //2、请求TrackerServer后返回的数据包含Storage的信息
        TrackerServer trackerServer = client.getConnection();

        //3、取出Storage信息,并且存储起来(storageClient)
        return new StorageClient(trackerServer,null);
    }


    /***
     * 文件删除
     * @throws Exception
     */
    public static int delete(String groupName, String remoteFileName) throws Exception{
        //1、请求TrackerServer
        StorageClient storageClient = getStorageClient();

        //4、文件删除
        int count = storageClient.delete_file(groupName, remoteFileName);
        return count;
    }

    /***
     * 获取Storage组信息
     * @throws Exception
     */
    public static StorageServer getStorages(String groupName) throws Exception{
        //1、请求TrackerServer
        TrackerClient client = new TrackerClient();

        //2、请求TrackerServer后返回的数据包含Storage的信息
        TrackerServer trackerServer = client.getConnection();

        //3、获取指定组的信息
        return client.getStoreStorage(trackerServer,groupName);
    }


    /***
     * 获取相关的Storage服务信息
     * @throws Exception
     */
    public static ServerInfo[] getServerInfo(String groupName, String remoteFileName) throws Exception{
        //1、请求TrackerServer
        TrackerClient client = new TrackerClient();

        //2、请求TrackerServer后返回的数据包含Storage的信息
        TrackerServer trackerServer = client.getConnection();

        //3、获取Storage信息
        return client.getFetchStorages(trackerServer,groupName,remoteFileName);
    }

    /****
     * 获取Tracker的信息
     * @throws Exception
     */
    public static String getTrackerUrl() throws Exception{
        //1、请求TrackerServer
        TrackerClient client = new TrackerClient();

        //2、请求TrackerServer后返回的数据包含Storage的信息
        TrackerServer trackerServer = client.getConnection();

        //获取Tracker相关信息  TCP 11211/Http 8080
        String url="Http://"+trackerServer.getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port();
        return url;
    }

    public static void main(String[] args) throws Exception{
        //获取文件信息测试
        //FileInfo fileInfo = getFile("group1","M00/00/00/wKjThF3668eASWAHAAnAAJuzIB4619.jpg");
        //System.out.println("IP:"+fileInfo.getSourceIpAddr());

        //文件下载
        //InputStream is = downFile("group1", "M00/00/00/wKjThF3668eASWAHAAnAAJuzIB4619.jpg");
        //创建字节输出流
        //FileOutputStream os = new FileOutputStream("D:/123.jpg");
        //byte[] buffer = new byte[1024];
        //while (is.read(buffer)!=-1){
        //    os.write(buffer);
        //}
        //关闭资源
        //is.close();
        //os.close();

        //删除文件
        //delete("group1", "M00/00/00/wKjThF3668eASWAHAAnAAJuzIB4619.jpg");

        //获取Storage信息
        //StorageServer group1 = getStorages("group1");
        //System.out.println(group1.getStorePathIndex());

        //获取指定文件所在的Storage组的信息
        //ServerInfo[] serverInfo = getServerInfo("group1", "M00/00/00/wKjThF369TOAA2BUAAnAAJuzIB4033.jpg");
        //for (ServerInfo info : serverInfo) {
        //    System.out.println(info.getIpAddr() + ":" + info.getPort());
        //}

        String trackerUrl = getTrackerUrl();
        System.out.println(trackerUrl);
    }


}
